package com.example.juc.lock;

import java.util.concurrent.TimeUnit;

public class Demo3 {
    public static void main(String[] args) {
        new Thread(new MyThread("lockA","lockB"),"A").start();
        new Thread(new MyThread("lockB","lockA"),"B").start();
    }
}

class MyThread implements Runnable{

    String lockA ;
    String lockB;
    public MyThread(String lockA,String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }


    @Override
    public void run() {
        synchronized (lockA){
            System.out.println(Thread.currentThread().getName()+"->");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lockB){
                System.out.println("hello");
            }
        }
    }
}